package cn.changeforyou.common.logFramework.logback.rolling.helper;

import org.apache.commons.lang3.StringUtils;

/**
 * 处理SignLogObject的工具
 */
public class SignLogUtil {

      static final String PRE = "{";
      static final String POST = "}";

      public static String SignLogObjectToSignLogString(SignLogObject signLogObject) {
            if(null == signLogObject) {
                  return null;
            }
            String sign = signLogObject.getSign();
            String body = signLogObject.getBody();
            if (StringUtils.isAnyBlank(sign, body)) {
                  return null;
            }
            return PRE + sign + POST + body;
      }

      public static String getSignFromSignLogString(String signLogString) {
            if(StringUtils.isBlank(signLogString)) {
                  return null;
            }
            int start = signLogString.indexOf(PRE);
            if(start < 0) {
                  return null;
            }
            int end = signLogString.indexOf(POST);
            if(end < 0) {
                  return null;
            }
            return signLogString.substring(start + 1, end);
      }

      public static String getSignMessageFromSignLogString(String signLogString) {
            if(StringUtils.isBlank(signLogString)) {
                  return null;
            }
            int start = signLogString.indexOf(PRE);
            if(start < 0) {
                  return null;
            }
            int end = signLogString.indexOf(POST);
            if(end < 0) {
                  return null;
            }
            return signLogString.substring(end + 1);
      }

      public static String[] getReFromSignLogString(String signLogString) {
            if(StringUtils.isBlank(signLogString)) {
                  return null;
            }
            int start = signLogString.indexOf(PRE);
            if(start < 0) {
                  return null;
            }
            int end = signLogString.indexOf(POST);
            if(end < 0) {
                  return null;
            }
            String[] re = new String[2];
            re[0] = signLogString.substring(start + 1, end);
            re[1] = signLogString.substring(end + 1);
            return re;
      }
}
